﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Media;
using Majoopic.Utility.Image;
using Majoopic.Utility;
using System.Diagnostics;

namespace Majoopic.UnitTest
{
    [TestClass]
    [DeploymentItem("\\Images\\")]
    public class ImageHeaderReaderTest
    {
        private TestContext _testContext;

        public TestContext TestContext
        {
            get { return _testContext; }
            set { _testContext = value; }
        }

        [TestMethod]
        public void TestImageDrawingReader()
        {
            //BitmapDecoder decoder = BitmapDecoder.Create(new FileStream("DSC_7637.jpg", FileMode.Open, FileAccess.Read), BitmapCreateOptions.None, BitmapCacheOption.Default);
            JpegBitmapDecoder decoder = new JpegBitmapDecoder(new FileStream("DSC_7637.jpg", FileMode.Open, FileAccess.Read), BitmapCreateOptions.None, BitmapCacheOption.None);
            BitmapMetadata metadata = decoder.Frames[0].Metadata as BitmapMetadata;
            object Xmpvalue = metadata.GetQuery("/xmp");
            object app1 = metadata.GetQuery("/app1/ifd/exif/{ushort=33434}");//858993459201
            object app13 = metadata.GetQuery("/app13/{ushort=0}/{ulonglong=61857348781037}");
            object app0 = metadata.GetQuery("/raw");
            Assert.IsNotNull(decoder);
        }

        [TestMethod()]
        public void TestWPFImagingReader()
        {
            ImageDecoder decoder = new ImageDecoder(DecoderType.WPFMetadataReader);
            ImageHeaderInformation headerInformation = decoder.GetImageHeaderInformation("DSC_7637.jpg");
            Assert.IsNotNull(headerInformation);
            string exposureTime = headerInformation.ExposureTime.ToString();
            string focallenght = headerInformation.FocalLenght.ToString();
            ImageHeaderInformation expected = new ImageHeaderInformation();
            expected.CameraModel = "NIKON D90";
            expected.DateTaken = new DateTime(2012, 12, 06, 15, 00, 20);
            expected.ExposureProgram = 3;
            expected.ExposureTime = new Rational(1, 200);
            expected.FlashMode = 0;
            expected.FocalLenght = new Rational(130, 1);
            expected.Height = 3392;
            expected.Width = 2253;
            expected.IsoSpeed = 250;
            expected.LensAperture = new Rational(71, 10);
            expected.LensModel = "18.0-200.0 mm f/3.5-5.6";

            this.AssertHeaderInformation(expected, headerInformation);

            headerInformation = decoder.GetImageHeaderInformation("IMAG0362.jpg");
            expected.CameraModel = "HTC One XL";
            expected.DateTaken = new DateTime(2013, 3, 21, 14, 35, 29);
            expected.ExposureProgram = null;
            expected.ExposureTime = new Rational(684, 1000000);
            expected.FlashMode = 0;
            expected.FocalLenght = new Rational(363, 100);
            expected.Height = 1840;
            expected.Width = 3264;
            expected.IsoSpeed = 100;
            expected.LensAperture = null;
            expected.LensModel = null;

            this.AssertHeaderInformation(expected, headerInformation);
        }

        private void AssertHeaderInformation(ImageHeaderInformation expected, ImageHeaderInformation source)
        {
            Assert.AreEqual(expected.CameraModel, source.CameraModel);
            Assert.AreEqual(expected.DateTaken, source.DateTaken);
            Assert.AreEqual(expected.ExposureProgram, source.ExposureProgram);
            Assert.AreEqual(expected.ExposureTime, source.ExposureTime);
            Assert.AreEqual(expected.FlashMode, source.FlashMode);
            Assert.AreEqual(expected.FocalLenght, source.FocalLenght);
            Assert.AreEqual(expected.Height, source.Height);
            Assert.AreEqual(expected.IsoSpeed, source.IsoSpeed);
            Assert.AreEqual(expected.LensAperture, source.LensAperture);
            Assert.AreEqual(expected.LensModel, source.LensModel);
            Assert.AreEqual(expected.Width, source.Width);
        }

        [TestMethod()]
        public void TestReadLotOfDirectory()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            TestContext.BeginTimer("Directory scan");
            List<string> files = FileUtility.GetAllImagesInDirectory(@"d:\", "jpg", true);
            watch.Stop();
            this.TestContext.EndTimer("Directory scan");
            this.TestContext.WriteLine("Timer : {0}",watch.Elapsed);
        }
    }
}